package cn.poco.pepsi.util;


import java.io.InputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class BitMapDecodeService {
	public Bitmap decodeStream(InputStream inputStream1 , InputStream inputStream2 , int requiredWidth){ 
	    try { 
	        BitmapFactory.Options o = new BitmapFactory.Options(); 
	        o.inJustDecodeBounds = true; 
	        BitmapFactory.decodeStream(inputStream1,null,o); 
	        final int REQUIRED_SIZE=requiredWidth; 
	        int width_tmp=o.outWidth;
	        int hight_tmp=o.outHeight;
	        inputStream1.close();
	        int scale=1; 
	        while(true){ 
	            if(width_tmp/2<REQUIRED_SIZE || hight_tmp/2<REQUIRED_SIZE) 
	                break; 
	            width_tmp/=2; 
	            hight_tmp/=2;
	            scale*=2; 
	        } 
//	        if (o.outHeight > requiredWidth || o.outWidth > requiredWidth) {    
//	        	scale = (int)Math.pow(2, (int) Math.round(Math.log(requiredWidth / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));     
//	        } 
	        BitmapFactory.Options o2 = new BitmapFactory.Options(); 
	        o2.inSampleSize=scale; 
	 //      Log.i("tt", scale+"");
	        Bitmap bitmap = BitmapFactory.decodeStream(inputStream2, null, o2);
	        inputStream2.close();
	        return  bitmap;
	    } catch (Exception e) {
	    } 
	    return null; 
	}
}
